#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, os.path, sys

import time
import tempfile
import unittest

from foxglove import log
from foxglove.jupacket import Packet,encodeRequest,decodeRequest,encodeResponse,decodeResponse


class   TestPacket(unittest.TestCase) :

    def test_request(self) :
        packet = Packet()
        packet.key = 'KEY--TEST--KEY'
        packet.timeout = 123456
        packet.status = 200
        packet.data = 'DATA##TEST##DATA'

        packet.setRequest()
        self.assertTrue(packet.isRequest())
        self.assertFalse(packet.isResponse())

        packet.setInsertAction()
        self.assertTrue(packet.isInsertAction())
        self.assertFalse(packet.isEraseAction())
        self.assertFalse(packet.isLookupAction())

        record = encodeRequest(packet)
        pkt = decodeRequest(record)
        self.assertTrue(pkt.isRequest())
        self.assertFalse(pkt.isResponse())
        self.assertTrue(pkt.isInsertAction())
        self.assertFalse(pkt.isEraseAction())
        self.assertFalse(pkt.isLookupAction())
        self.assertEqual(pkt.key, packet.key)
        self.assertEqual(pkt.action, packet.action)
        self.assertEqual(pkt.timeout, packet.timeout)
        self.assertEqual(pkt.data, packet.data)
        # REQUEST ignore STATUS field
        ##self.assertEqual(pkt.status, packet.status)

        packet.setEraseAction()
        self.assertFalse(packet.isInsertAction())
        self.assertTrue(packet.isEraseAction())
        self.assertFalse(packet.isLookupAction())

        record = encodeRequest(packet)
        pkt = decodeRequest(record)
        self.assertTrue(pkt.isRequest())
        self.assertFalse(pkt.isResponse())
        self.assertFalse(pkt.isInsertAction())
        self.assertTrue(pkt.isEraseAction())
        self.assertFalse(pkt.isLookupAction())
        self.assertEqual(pkt.key, packet.key)
        self.assertEqual(pkt.action, packet.action)
        # ERASE REQUEST ignore TIMEOUT field
        ##self.assertEqual(pkt.timeout, packet.timeout)
        # ERASE REQUEST ignore DATA field
        ##self.assertEqual(pkt.data, packet.data)
        # REQUEST ignore STATUS field
        ##self.assertEqual(pkt.status, packet.status)

        packet.setLookupAction()
        self.assertFalse(packet.isInsertAction())
        self.assertFalse(packet.isEraseAction())
        self.assertTrue(packet.isLookupAction())

        record = encodeRequest(packet)
        pkt = decodeRequest(record)
        self.assertTrue(pkt.isRequest())
        self.assertFalse(pkt.isResponse())
        self.assertFalse(pkt.isInsertAction())
        self.assertFalse(pkt.isEraseAction())
        self.assertTrue(pkt.isLookupAction())
        self.assertEqual(pkt.key, packet.key)
        self.assertEqual(pkt.action, packet.action)
        # LOOKUP REQUEST ignore TIMEOUT field
        ##self.assertEqual(pkt.timeout, packet.timeout)
        # LOOKUP REQUEST ignore DATA field
        ##self.assertEqual(pkt.data, packet.data)
        # REQUEST ignore STATUS field
        ##self.assertEqual(pkt.status, packet.status)
        # test_request()

    def test_response(self) :
        packet = Packet()
        packet.key = 'KEY--TEST--KEY'
        packet.timeout = 123456
        packet.status = 200
        packet.data = 'DATA##TEST##DATA'

        packet.setResponse()
        self.assertFalse(packet.isRequest())
        self.assertTrue(packet.isResponse())

        packet.setInsertAction()
        self.assertTrue(packet.isInsertAction())
        self.assertFalse(packet.isEraseAction())
        self.assertFalse(packet.isLookupAction())

        record = encodeResponse(packet)
        pkt = decodeResponse(record)
        self.assertFalse(pkt.isRequest())
        self.assertTrue(pkt.isResponse())
        self.assertTrue(pkt.isInsertAction())
        self.assertFalse(pkt.isEraseAction())
        self.assertFalse(pkt.isLookupAction())
        self.assertEqual(pkt.key, packet.key)
        self.assertEqual(pkt.action, packet.action)
        self.assertEqual(pkt.timeout, packet.timeout)
        # RESPONSE ignore DATA field
        ##self.assertEqual(pkt.data, packet.data)
        self.assertEqual(pkt.status, packet.status)

        packet.setEraseAction()
        self.assertFalse(packet.isInsertAction())
        self.assertTrue(packet.isEraseAction())
        self.assertFalse(packet.isLookupAction())

        record = encodeResponse(packet)
        pkt = decodeResponse(record)
        self.assertFalse(pkt.isRequest())
        self.assertTrue(pkt.isResponse())
        self.assertFalse(pkt.isInsertAction())
        self.assertTrue(pkt.isEraseAction())
        self.assertFalse(pkt.isLookupAction())
        self.assertEqual(pkt.key, packet.key)
        self.assertEqual(pkt.action, packet.action)
        # RESPONSE ignore TIMEOUT field
        ##self.assertEqual(pkt.timeout, packet.timeout)
        # RESPONSE ignore DATA field
        ##self.assertEqual(pkt.data, packet.data)
        self.assertEqual(pkt.status, packet.status)

        packet.setLookupAction()
        self.assertFalse(packet.isInsertAction())
        self.assertFalse(packet.isEraseAction())
        self.assertTrue(packet.isLookupAction())

        record = encodeResponse(packet)
        pkt = decodeResponse(record)
        self.assertFalse(pkt.isRequest())
        self.assertTrue(pkt.isResponse())
        self.assertFalse(pkt.isInsertAction())
        self.assertFalse(pkt.isEraseAction())
        self.assertTrue(pkt.isLookupAction())
        self.assertEqual(pkt.key, packet.key)
        self.assertEqual(pkt.action, packet.action)
        # RESPONSE ignore TIMEOUT field
        ##self.assertEqual(pkt.timeout, packet.timeout)
        self.assertEqual(pkt.data, packet.data)
        self.assertEqual(pkt.status, packet.status)

        # test_response

    # class TestPacket

#######################################################################

def test_suite():
    suite = unittest.TestSuite()

    suite.addTest(unittest.makeSuite(TestPacket))

    return suite

if  __name__ == '__main__' :
    #log.setLogPath('/dev/null')
    log.enableDebug()

    unittest.main()
